/** 
 * File: AbstractLessonController.java
 * @author: Adam Phung
 * Date Created:
 * Date Modified: Dec 6, 2012 
 * Description:
 */
package edu.bu.cs673.superkids123.children.controller;

import java.util.Date;
import java.util.List;

import edu.bu.cs673.superkids123.AbstractController;
import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.children.DataObjectAccess.ReportManager;
import edu.bu.cs673.superkids123.children.model.CompletedLesson;
import edu.bu.cs673.superkids123.children.model.Lesson;
import edu.bu.cs673.superkids123.children.view.lessons.LessonResultsView;
import edu.bu.cs673.superkids123.common.model.Achievement;
import edu.bu.cs673.superkids123.common.model.Child;

/**
 * The Class AbstractLessonController.
 */
public abstract class AbstractLessonController extends AbstractController{

	private boolean active;
	
	private Lesson lesson;
	private CompletedLesson completedLesson;
	private List<Achievement> earnedAchievements;
	
	/**
	 * Instantiates a new abstract lesson controller.
	 *
	 * @param mainFrame the main frame
	 */
	public AbstractLessonController(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	/**
	 * Start a new lesson record.
	 *
	 * @param lesson
	 */
	public void start(Lesson l) {
		active = true;
		lesson = l;
		completedLesson = new CompletedLesson();
		completedLesson.setLesson(lesson);
		completedLesson.setStartDate(new Date());
		Child child = (Child) authController.getCurrentUser();
		completedLesson.setChild(child);
		completedLesson.setCompleted(false);
	}
	
	/**
	 * Stops lesson recording, assumes lesson was terminated abruptly.
	 */
	public void stop() {
		stop(false);
	}
	
	/**
	 * Stops lesson recording, pass true for completion.
	 *
	 * @param complete boolean, true is completed
	 */
	public void stop(boolean complete) {
		active = false;
		completedLesson.setEndDate(new Date());
		completedLesson.setCompleted(complete);
		
    	// Save to DB
    	ReportManager.addCompletedLesson(completedLesson);
    	
    	// Award Achievements
    	earnedAchievements = getMainFrame().getController(AchievementController.class).awardAchievements();
	}
	
	/**
	 * Gets the earned achievements.
	 *
	 * @return the earned achievements
	 */
	public List<Achievement> getEarnedAchievements() {
		return earnedAchievements;
	}
	
	/**
	 * Returns true if an active lesson is occurring.
	 * @return boolean
	 */
	public boolean isActive() {
		return active;
	}
}
